//
//  ZLRSegmentControl.m
//  feng
//
//  Created by 张领瑞 on 2017/9/11.
//  Copyright © 2017年 ShiFeiEr. All rights reserved.
//

#import "ZLRSegmentControl.h"

#import <Masonry/Masonry.h>

@interface ZLRSegmentControl ()

@property (nonatomic,strong)UIView *view;

@end

@implementation ZLRSegmentControl

-(instancetype)initWithFrame:(CGRect)frame titileArray:(NSArray *)array{
    self = [super initWithFrame:frame];
    float width = CGRectGetWidth(frame);
    float height = CGRectGetHeight(frame);
    self.titlesArray = array;
    if (self) {
        long num = self.titlesArray.count;
        if (num > 1) {
            for (int i = 0; i < num; i++) {
                UIButton * but = [UIButton buttonWithType:UIButtonTypeCustom];
                but.frame = CGRectMake(i*width/num, 0, width/num, height);
                but.tag = 100+i;
                [but addTarget:self action:@selector(didSelected:) forControlEvents:UIControlEventTouchUpInside];
                [but setTitle:self.titlesArray[i] forState:UIControlStateNormal];
                [but setTitleColor:[UIColor colorWithRed:0x17/255.0  green:0x07/255.0 blue:0x0A/255.0 alpha:1] forState:UIControlStateNormal];
                if (i == 0) {
                    but.selected = YES;
                }
                [but setTitleColor:[UIColor colorWithRed:0xeb/255.0 green:0x4f/255.0 blue:0x6f/255.0 alpha:1] forState:UIControlStateSelected];
                [self addSubview:but];
                [self setUnderlineNone:but];//去掉title的下划线
                
            }
            
            for (int i = 1; i < num; i++) {
            UIView * view = [[UIView alloc] initWithFrame:CGRectMake(i*width/num, height/4, 2, height/2)];
                view.backgroundColor = [UIColor colorWithRed:0xF0/255.0  green:0xEB/255.0 blue:0xEB/255.0 alpha:1];
                [self addSubview:view];
            }
            _view = [[UIView alloc] initWithFrame:CGRectMake(0, height-2, width/num, 2)];
            _view.backgroundColor = [UIColor colorWithRed:0xeb/255.0 green:0x4f/255.0 blue:0x6f/255.0 alpha:1];
            [self addSubview:_view];
        }
    }
    return self;
}

-(void)didSelected:(UIButton*)sender{
    
    
    long i = sender.tag - 100;
   
    [self setSelectedAtIndex:i];

    //执行代理方法
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectedAtIndext:)]) {
        [self.delegate didSelectedAtIndext:i];
    }
    
}

-(void)setSelectedAtIndex:(NSInteger)index{
    float width = self.frame.size.width;
    float height = self.frame.size.height;
    long num = self.titlesArray.count;
    for (id obj in self.subviews) { //设置所以button的选择状态：未选中
        if ([obj isKindOfClass:[UIButton class]]) {
            UIButton *but = obj;
            but.selected = NO;
        }
    }
    UIButton *but = [self viewWithTag:100+index];
    but.selected = YES;
    //给下划线添加动画
    [UIView animateWithDuration:0.2 animations:^{
        _view.frame = CGRectMake(index*width/num, height-2, width/num, 2);
    }];
}
-(int)currentSelectedAtIndex{
    int index = -1;
    for (id obj in self.subviews) { //遍历所有button
        if ([obj isKindOfClass:[UIButton class]]) {
            UIButton *but = obj;
            if (but.selected ==YES) {
                index = (int)but.tag - 100;
            }
        }
    }
    return index;
}
 //去掉title的下划线
-(void)setUnderlineNone:(UIButton*)sender {
    if (sender) {
        NSString *text = sender.titleLabel.text;
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:text];
        
        [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleNone] range:NSMakeRange(0,text.length)];
        [sender setAttributedTitle:str forState:UIControlStateNormal];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
